#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import sys

from common.process_handle import process_handle
from common.common import exter_attack
from adds.comm.log import log
from adds.service.resource.adds_resource_service import ADDSResourceService
from adds.service.backup.adds_backup_service import ADDSBackupService
from adds.service.restore.adds_restore_service import ADDSRestoreService


class ADDSApi:
    @staticmethod
    @exter_attack
    def check_application(req_id, job_id, sub_id, data):
        ADDSResourceService(req_id, job_id, sub_id).check_application()

    @staticmethod
    @exter_attack
    def query_job_permission(req_id, job_id, sub_id, data):
        ADDSResourceService(req_id, job_id, sub_id).query_job_permission()

    @staticmethod
    @exter_attack
    def allow_backup_in_local_node(req_id, job_id, sub_id, data):
        ADDSBackupService(req_id, job_id, sub_id).allow_backup_in_local_node()

    @staticmethod
    @exter_attack
    def check_backup_job_type(req_id, job_id, sub_id, data):
        ADDSBackupService(req_id, job_id, sub_id).check_backup_job_type()

    @staticmethod
    @exter_attack
    def backup_prerequisite(req_id, job_id, sub_id, data):
        ADDSBackupService(req_id, job_id, sub_id).backup_prerequisite()

    @staticmethod
    @exter_attack
    def backup_prerequisite_progress(req_id, job_id, sub_id, data):
        ADDSBackupService(req_id, job_id, sub_id).backup_prerequisite_progress()

    @staticmethod
    @exter_attack
    def backup_gen_sub_job(req_id, job_id, sub_id, data):
        ADDSBackupService(req_id, job_id, sub_id).backup_gen_sub_job()

    @staticmethod
    @exter_attack
    def backup(req_id, job_id, sub_id, data):
        ADDSBackupService(req_id, job_id, sub_id).backup()

    @staticmethod
    @exter_attack
    def backup_post_job(req_id, job_id, sub_id, data):
        ADDSBackupService(req_id, job_id, sub_id).backup_post_job()

    @staticmethod
    @exter_attack
    def backup_post_job_progress(req_id, job_id, sub_id, data):
        ADDSBackupService(req_id, job_id, sub_id).backup_post_job_progress()

    @staticmethod
    @exter_attack
    def query_backup_copy(req_id, job_id, sub_id, data):
        ADDSBackupService(req_id, job_id, sub_id).query_backup_copy()

    @staticmethod
    @exter_attack
    def abort(req_id, job_id, sub_id, data):
        ADDSBackupService(req_id, job_id, sub_id).abort_backup_job()
        
    @staticmethod
    @exter_attack
    def allow_restore_in_local_node(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).allow_restore_in_local_node()

    @staticmethod
    @exter_attack
    def restore_prerequisite(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_prerequisite()

    @staticmethod
    @exter_attack
    def restore_prerequisite_progress(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_prerequisite_progress()

    @staticmethod
    @exter_attack
    def restore_gen_sub_job(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_gen_sub_job()

    @staticmethod
    @exter_attack
    def restore(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore()

    @staticmethod
    @exter_attack
    def restore_post_job(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_post_job()

    @staticmethod
    @exter_attack
    def restore_post_job_progress(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_post_job_progress()

    @staticmethod
    @exter_attack
    def allow_restore_in_local_node(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).allow_restore_in_local_node()

    @staticmethod
    @exter_attack
    def restore_prerequisite(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_prerequisite()

    @staticmethod
    @exter_attack
    def restore_prerequisite_progress(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_prerequisite_progress()

    @staticmethod
    @exter_attack
    def restore_gen_sub_job(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_gen_sub_job()

    @staticmethod
    @exter_attack
    def restore(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore()

    @staticmethod
    @exter_attack
    def restore_post_job(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_post_job()

    @staticmethod
    @exter_attack
    def restore_post_job_progress(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_post_job_progress()

    @staticmethod
    @exter_attack
    def allow_restore_in_local_node(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).allow_restore_in_local_node()

    @staticmethod
    @exter_attack
    def restore_prerequisite(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_prerequisite()

    @staticmethod
    @exter_attack
    def restore_prerequisite_progress(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_prerequisite_progress()

    @staticmethod
    @exter_attack
    def restore_gen_sub_job(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_gen_sub_job()

    @staticmethod
    @exter_attack
    def restore(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore()

    @staticmethod
    @exter_attack
    def restore_post_job(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_post_job()

    @staticmethod
    @exter_attack
    def restore_post_job_progress(req_id, job_id, sub_id, data):
        ADDSRestoreService(req_id, job_id, sub_id).restore_post_job_progress()


# 功能映射字典
fun = {
    'CheckApplication': ADDSApi.check_application,

    'QueryJobPermission': ADDSApi.query_job_permission,

    'AllowBackupInLocalNode': ADDSApi.allow_backup_in_local_node,
    'CheckBackupJobType': ADDSApi.check_backup_job_type,
    'BackupPrerequisite': ADDSApi.backup_prerequisite,
    'BackupPrerequisiteProgress': ADDSApi.backup_prerequisite_progress,
    'BackupGenSubJob': ADDSApi.backup_gen_sub_job,
    'Backup': ADDSApi.backup,
    'BackupPostJob': ADDSApi.backup_post_job,
    'BackupPostJobProgress': ADDSApi.backup_post_job_progress,
    'QueryBackupCopy': ADDSApi.query_backup_copy,
    'AbortJob': ADDSApi.abort,

    'AllowRestoreInLocalNode': ADDSApi.allow_restore_in_local_node,
    'RestorePrerequisite': ADDSApi.restore_prerequisite,
    'RestorePrerequisiteProgress': ADDSApi.restore_prerequisite_progress,
    'RestoreGenSubJob': ADDSApi.restore_gen_sub_job,
    'Restore': ADDSApi.restore,
    'RestorePostJob': ADDSApi.restore_post_job,
    'RestorePostJobProgress': ADDSApi.restore_post_job_progress,

}

# ADDS Plugin API 入口
if __name__ == '__main__':
    """
    功能描述:执行ADDS插件主程序
    参数:
    @1: 请求类型
    @2: 请求的ID
    @3: 主任务ID
    @4: 子任务ID
    输入:
    标准输入数据为外部程序通过管道向插件程序注入的各项参数数据（可能携带敏感数据）
    样例:
    python3 adds_api.py Backup 1657539915784 2e0c6c0b faa3e2c0
    """
    # 打印日志
    log.info('Enter ADDS Plugin process')
    process_handle(log, sys.argv, sys.stdin, fun)
    log.info('Exit ADDS Plugin process')
